﻿using Direct.IDAL.User.Money;
using DirectSell.Model.Money;
using DirectSell.Model.Query;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.DAL.User.Money
{
    /// <summary>
    /// 现金币操作类
    /// </summary>
    public class RealMoneyRepository:AbsRepository, IRealMoneyRepository
    {

        #region 申请现金币
        /// <summary>
        /// 申请现金币
        /// </summary>
        /// <returns></returns>
        public void Apply(RealMoneyReq req,ref Hashtable hsTable)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" INSERT INTO RealMoneyApply ");
            sb.Append(" ([BankName] ");
            sb.Append(" ,[BankCode] ");
            sb.Append(" ,[UserName] ");
            sb.Append(" ,[CardHolder] ");
            sb.Append(" ,[Amount] ");
            sb.Append(" ,[ApplyTime] ");
            sb.Append(" ,[Remark] ");
            sb.Append(" ,[State] ");
            sb.Append(" ,[UserCode] ");
            sb.Append(" ,[RealAmount]) ");
            sb.Append(" VALUES ");
            sb.AppendFormat(" ('{0}' ",req.BankName);
            sb.AppendFormat("  ,'{0}' ", req.BankCode);
            sb.AppendFormat("  ,'{0}' ", req.UserName);
            sb.AppendFormat(" ,'{0}' ", req.CardHolder);
            sb.AppendFormat(" ,{0} ", req.Amount);
            sb.AppendFormat(" ,'{0}' ", req.ApplyTime);
            sb.AppendFormat(" ,'{0}' ", req.Remark);
            sb.AppendFormat(" ,{0} ", req.State);
            sb.AppendFormat(" ,'{0}' ", req.UserCode);
            sb.AppendFormat(" ,{0}) ", req.RealAmount);
            hsTable.Add(sb.ToString(), null);
        }
        #endregion

        #region 查询申请记录
        /// <summary>
        /// 查询申请记录
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public async Task<PagedList<RealMoney>> Record(string usercode,int pageindex,int pagesize)
        {
            PageParam model = new PageParam()
            {
                OrderBy = "Id DESC",
                PageIndex = pageindex,
                PageSize = pagesize,
                StrWhere = $" UserCode='{usercode}'",
                TableName = "RealMoneyApply"
            };
            return await base.PageQuery<RealMoney>(model);
        }
        #endregion

        #region 查询余额
        /// <summary>
        /// 查询余额
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public  decimal QueryBalance(string usercode)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT RealMoney FROM UserInforMations ");
            sb.AppendFormat(" where UserCode='{0}' ", usercode);
            return base.QueryFirstT<decimal>(sb.ToString());
        }
        #endregion

        #region 计算实际到账金额
        /// <summary>
        /// 计算实际到账
        /// </summary>
        /// <param name="money"></param>
        /// <returns></returns>
        public async Task<decimal> Compute(decimal money)
        {
            var charge = await QueryServiceCharge();
            return money-money * charge;
        }
        #endregion

        #region 查询提现手续费
        /// <summary>
        /// 查询提现手续费
        /// </summary>
        /// <returns></returns>
        public async Task<decimal> QueryServiceCharge()
        {
            string sql = " SELECT ServiceCharge FROM Params WHERE Id=1 ";
            return await base.QueryFirstTAsync<decimal>(sql);
        }
        #endregion

        #region 写入消费表
        /// <summary>
        /// 写入消费表
        /// </summary>
        /// <param name="req"></param>
        /// <param name="msg"></param>
        /// <param name="type"></param>
        /// <param name="hstable"></param>
        public void InsertConsumption(RealMoneyReq req, ref Hashtable hstable)
        {
            Consumption model = new Consumption()
            {
                Affair = "申请提现",
                Amout = req.Amount,
                CreateDate = req.ApplyTime,
                LogType = 0,
                Operator = req.UserCode,
                Relevant = req.UserCode
            };
            base.InsertConsumption(model, ref hstable);
        }
        #endregion

        #region 扣费
        /// <summary>
        /// 扣分
        /// </summary>
        /// <returns></returns>
        public void Deductions(RealMoneyReq req,ref Hashtable hstable)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" UPDATE UserInforMations SET  ");
            sb.AppendFormat(" RealMoney-={0} where UserCode='{1}'", req.Amount, req.UserCode);
            hstable.Add(sb.ToString(), null);
        }
        #endregion

    }
}
